{
数据结构 解题报告：
http://git.oschina.net/et0662/sznoi-Data_Structure
}
type
	pnode=^node;
	node=record
		data:longint;
		left:pnode;
		right:pnode;
		end;
	tree=node;
	ptree=pnode;

{先序}
procedure lpt(t:ptree);
begin
	if(t<>nil)then
	begin
		write(t^.data,' ');
		lpt(t^.left);
		lpt(t^.right);
	end;
end;

{中序}
procedure mpt(t:ptree);
begin
	if(t<>nil)then
	begin
		mpt(t^.left);
		write(t^.data,' ');
		mpt(t^.right);
	end;
end;

{后序}
procedure rpt(t:ptree);
begin
	if(t<>nil)then
	begin
		rpt(t^.left);
		rpt(t^.right);
		write(t^.data,' ');
	end;
end;

{清空树}
procedure emptyTree(t:ptree);
begin
	if(t<>nil)then
	begin
		emptyTree(t^.left);
		emptyTree(t^.right);
		dispose(t);
	end;
end;

{2^8 都不会超过integer,这里就只用integer}
function pow(a,b:integer):integer;
var
	i,sum:integer;
begin
	if(b=0)then
		sum:=1
	else
	begin
		sum:=1;
		for i:=1 to b do
			sum:=sum*a;
	end;
	pow:=sum;
end;

var
	a: array of pNode;
	p: pNode;
	t: pTree;
	i,j,deep,ss,nNode:integer;
	flag:boolean;
	temp:longint;
begin
	readln(deep,ss);	{读入深度 和顺序}
	nNode:=pow(2,deep)-1;	{计算结点}
	setlength(a,nNode);
	a[0]:=nil;

	{去重并读入}
	i:=0;
	while(i<>nNode)do
	begin
		read(temp);
		flag:=true;
		for j:=0 to i-1 do
		begin
			if a[j]^.data=temp then
			begin
				flag:=false;
				break;
			end;
		end;
		
		if not flag then continue;

		new(p);
		p^.left:=nil;
		p^.right:=nil;
		p^.data:=temp;
		a[i]:=p;
		inc(i);
	end;

	{构建树}
	i:=0;j:=1;
	t:=a[0];
	p:=a[0];
	while(j<nNode)do
	begin
		if(p<>nil)then
		begin
			p^.left:=a[j];
			inc(j);
			p^.right:=a[j];
			inc(j);
			inc(i);
			p:=a[i];
		end;
	end;

	if(ss=1)then
		lpt(t)
	else if(ss=2) then
		mpt(t)
	else
		rpt(t);
	emptyTree(t);
end.
